﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace ComicSharp.Helpers
{
    public static class FileHelper
    {
        /// <summary>
        /// Gets the seven zip library path.
        /// </summary>
        /// <returns>The full file path to the library.</returns>
        public static string GetSevenZipLibraryPath()
        {
            // Get the program's install path
            string appPath = Application.ExecutablePath.ToString();

            // Replace the program's main exe with the file path to the library
            appPath = appPath.Replace(Path.GetFileName(appPath), @"Library\7z.dll");

            // Return full path
            return appPath;
        }

        /// <summary>
        /// Generates the random temp folder.
        /// </summary>
        public static void GenerateRandomTempFolder()
        {
            // Generate random path in the My Documents folder
            string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\ComicSharp\\" + Path.GetRandomFileName();

            // Delete the directory if it already exists
            if (Directory.Exists(path))
            {
                Directory.Delete(path, true);
            }

            // Create the directory
            Directory.CreateDirectory(path);

            // Save to global variable
            Program.tempPath = path;  
        }

        /// <summary>
        /// Gets the comic book pages from the extracted archive. Method will parse down to the lowest subdirectory
        /// to retrieve the images since some archives contain nested folders.
        /// </summary>
        /// <param name="path">The path to the root directory.</param>
        /// <returns>A string array containing the full path of each comic page.</returns>
        public static string[] GetPages(string path)
        {
            // Declare variables
            Stack<string> stack = new Stack<string>();
            string [] files = null;
            string[] directories = null;
            string currentDir = string.Empty;

            // Add the top directory to the stack
            stack.Push(path);

            while(stack.Count > 0)
            {
                // Pop the top directory
                currentDir = stack.Pop();

                // Check to see if directory has subdirectories
                directories = Directory.GetDirectories(currentDir);

                // If subdirectories exist
                if(directories.Length > 0)
                {
                    // Add each directory to the stack
                    foreach (string directory in directories)
                    {
                        stack.Push(directory);
                    }
                }
                else
                {
                    // No more directories, add the files
                    files = Directory.GetFiles(currentDir);
                }
            }

            return files;
        }

        /// <summary>
        /// Deletes the temp folder and all its contents.
        /// </summary>
        public static void DeleteTempFolder()
        {
            if(!string.IsNullOrEmpty(Program.tempPath))
            {
                Directory.Delete(Program.tempPath, true);
            }
        }
    }
}
